var ctx;
/*var pwdReg = /^(?![^a-zA-Z]+$)(?!\D+$).{8,30}$/; //至少包含字母数字的验证
*/
var pwdReg = /^\S{6,15}$/;

function setCtx(ctx) {
	this.ctx = ctx;
}


var lname = $("#loginName").val();
var sixCode;
var phoneCode;
var timer;
//每隔60s发送一次验证码
	$("#sendcode").on("click",function(){
		resendTime();
			$.ajax({
                type : "POST",
                url: ctx + "/sendCode",
                data:{
                	lname:lname
                	},
                cache:false,
                async: false,
                dataType:'json',
                success:function(result) {
					console.info(result);
					if (result.success) {
						layer.msg("验证码发送成功",{
							icon: 1,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
						$("#sixCode").val(result.obj);
						sixCode = $("#sixCode").val();
						
						loadContent("/findPassword");
					} else {
						layer.msg("验证码发送失败",{
							icon: 2
						});
					}
                }
			});	
			})

function resendTime(){
	 var phone=$("#phone").val();
	 
	    if(phone!="" && timer == null){
	        curTime=$("#resendTime").val();
	        $("#sendcode").attr("disabled",true);
	        $("#sendcode").css("background","lightgrey");
	        $("#warning").text(curTime+"秒后可重发");
	        timer=setInterval(handleTimer, 1000);
	    }
}
function handleTimer(){
    if(curTime==0){
        clearInterval(timer);
        timer=null;
        $("#sendcode").removeAttr("disabled");
        $("#sendcode").css("background","#ffaa00");
        $("#warning").text("重发验证码");
    }
    else{
    	
        curTime--;
        $("#warning").text(curTime+"秒后可重发");
       
    }
}


//验证验证码
function checkCode(){
	var code = $("#phoneCode").val();
	//解决ie9以下placeholder与value重复的问题
	if(code == $("#phoneCode").attr("placeholder")){
		code = "";
	}

	if(code == null || code == ""){
		layer.tips('请输入验证码！', '#phoneCode', {
		    tips: 3,
		    time:1000
		});
		return false;
	} else if( code != sixCode) {
		layer.tips('验证码输入错误，请重新输入！', '#phoneCode', {
		    tips: 3,
		    time:1000
		});
		return false;
	} else {
		return true;
	}
} 

//验证新密码
function checkNewPwd(){
	var newPwd = $("#newPwd").val();
	//解决ie9以下placeholder与value重复的问题
	if(newPwd == $("#newPwd").attr("placeholder")){
		newPwd = "";
	}
	if(newPwd == null || newPwd == ""){
		layer.tips('请输入密码！', '#newPwd', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		if(!pwdReg.test(newPwd)){
			layer.tips('密码格式为6-15位位非空字符!', '#newPwd', {
			    tips: 3,
			    time:1000
			});
			return false;
		}else{
			return true;
		}
	}	
}

//再次验证新密码
function checkNewPwd1(){
	var newPwd1 = $("#newPwd1").val();
	var newPwd = $("#newPwd").val();
	//解决ie9以下placeholder与value重复的问题
	if(newPwd1 == $("#newPwd1").attr("placeholder")){
		newPwd1 = "";
	}

	if(newPwd1 == null || newPwd1 == ""){
		layer.tips('请输入密码', '#newPwd1', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		if(!pwdReg.test(newPwd1)){
			layer.tips('密码格式为6-15位位非空字符!', '#newPwd1', {
			    tips: 3,
			    time:1000
			});
			return false;
		}else{
			if(newPwd != newPwd1){
				layer.tips('确认密码输入不一致，请检查输入的密码!', '#newPwd1', {
				    tips: 3,
				    time:1000
				});
				return false;
			}else{
				return true;
			}
		}
	}
}

//修改密码
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
function savePassword (){
	var userId = $("#userId").val();
	var loginName = $("#loginName").val();
	var newPwd = $("#newPwd").val();
	
	if(checkCode() && checkNewPwd() && checkNewPwd1()){
		newPwd = encrypt(newPwd,loginName);
		var index1 = layer.load(1);
		$.post(ctx+"/completePassword",
				{
			id:userId,
			loginName:loginName,
			newPwd:newPwd
				},function(data){
					data = JSON.parse(data);
					if(data.success){
						layer.msg("操作成功",{
							icon: 1,
							time: 1000 //2秒关闭（如果不配置，默认是3秒）
						},function(){
							layer.close(index1);
							window.history.back(-1); 
							parent.layer.close(index);
							
						});
					} else {
						layer.msg(data.msg,{
							icon: 2,
							time: 1000 //2秒关闭（如果不配置，默认是3秒）
						},
						function(){
							layer.close(index1);
							
						});
					}
				});
		
	}
}

